// pages/advertise/advertise.js
const app = getApp();
import {
    GET
} from '../../utils/methods';

Page({

    /**
     * 页面的初始数据
     */
    data: {   
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        let that = this
        if (!app.globalData.userList) {
            app.login().then(() => {
                    if(!app.globalData.userList){
                        app.login().then(()=>{
                            that.initData(options)
                        })
                    }
                });
        }else{
            that.initData(options)
        }
 
    },

    initData(options){
        let that = this
        var url = decodeURIComponent(options.url)
        
        if(options.category&&options.category==5){
          let ceurl = url.split('list.html')[0]
          url = ceurl + 'index.html#/scale/onlinEevaluation'
          if (url.indexOf('127.0.0.1')>-1) {
            url = url.split('127.0.0.1')[0] + 'localhost' + url.split('127.0.0.1')[1]
          }
        }
        console.log("aaaaaaaa",url);
        
        if(url&&url.indexOf('qmx')==-1){
            url = url + (url && url.indexOf('?') > -1 ? '&' : '?') + "openId=" + app.globalData.openid + "&phoneNo=" + app.globalData.userMobile
        }
        console.log("bbbbbb",url);
       
           
            let userId =app.globalData.userId
    
            let userName=""
            let tenantId = ""
            let gender=""
            let birthday=""
            let deptId= ""
            let healthCheckNo=""
            if(app.globalData.userList&&app.globalData.userList.length){
                let user= app.globalData.userList.filter(item=>{
                    return item.id == userId
                })[0];
                console.log('user',user)
                userName= user.userName
                tenantId= user.tenantId
                gender=user.gender
                birthday=user.birthday
                deptId=user.deptId
                healthCheckNo=user.healthCheckNo
            }
           

            //消息中点击量表推送
            if (url.indexOf('scale/onlinEevaluation')>-1) {
            
              let {
                  queueId,
                  account
              } = app.globalData
              url = url + `&patientId=${ userId }&patientName=${ userName }&tenantId=${ tenantId }&gender=${ gender }&birthday=${ birthday }&queueId=${queueId}&accountId=${account}&t=${new Date().getTime()}`
              console.log('量表组合1',url)
          }
           
           

        /**
         * 2021/4/30
         * 如果是照片分享的话
         */
        if (url&&url.indexOf('photoShare/photoShareList') > -1) {
            url = decodeURIComponent(options.url)
            url = url.replace('index.html/','index.html#/')
            console.log('url',url)
            url =app.globalData.config.webServer+ '/'+url+ `?id=${ userId }&tenantId=${ tenantId }&messageNotifyId=${options.messageNotifyId}`
            //   url = `${ 'http://localhost:9020/#/photoShare/photoShareList' }?id=${ userId }&tenantId=${ tenantId }&messageNotifyId=${options.messageNotifyId}`
            // url = `${ url }${ url.indexOf('?') > -1 ? '&' : '?' }id=${ userId }&tenantId=${ tenantId }`
            console.log('照片分享',url)
        }
        /**
         * 2021/3/19 
         * 如果是月度评估则添加 code 和 队列id
         */
        if (url&&url.indexOf('babyEvaluate') > -1) {
            let {
                code,
                queueId,
                currentPatient
            } = app.globalData
            const {
                userName,
                phoneNo,
                birthday,
                pregnancyDay,
                gender,
                id
            } = currentPatient
            url = decodeURIComponent(options.url)
            url = `${ options.url }?code=${ code }&state=${ queueId }&name=${ userName }&phone=${ phoneNo }&birthday=${ birthday }&pregnancyDay=${ pregnancyDay }&gender=${ gender }&id=${ id }`
        }
        /**
         * 2021/5/10
         * 患者端预约
         */
        if (url&&url.indexOf('checkReminder') > -1) {
            url = `${ url }&messageId=${ options.messageId }&messageNotifyId=${ options.messageNotifyId }`
        }

        /**
         * 2021/10/26
         * 量表组合
         */
        if (options.url.indexOf('scale/onlinEevaluation')>-1) {
            
            // url =app.globalData.config.webServer+url+ `&patientId=${ userId }&patientName=${ userName }&tenantId=${ tenantId }&gender=${ gender }&birthday=${ birthday }&t=${new Date().getTime()}`
            let {
                queueId,
                account
            } = app.globalData
            url = app.globalData.config.webServer + url + `&patientId=${ userId }&patientName=${ userName }&tenantId=${ tenantId }&gender=${ gender }&birthday=${ birthday }&queueId=${queueId}&accountId=${account}&t=${new Date().getTime()}`

            // url ='http://localhost:9020/#/scale/onlinEevaluation'+ `?phoneNo=17638136170&patientId=${ userId }&patientName=${ userName }&tenantId=${ tenantId }&gender=${ gender }&birthday=${ birthday }&t=${new Date().getTime()}`
            console.log('量表组合1',url)
        }

        



        /**
         * 报告列表
         */
        if (options.type === 'reportList') {
            url = decodeURIComponent(options.url)
            let {
                currentPatient,
                hospitalInfo
            } = app.globalData
         
            console.log('hospitalInfo', hospitalInfo)
            let {
                id,
                accountId,
                tenantId,
                birthday
            } = currentPatient
            let {
                hospName
            } = hospitalInfo

            url = `${ url }?timestamp=${new Date().getTime()}&patientId=${ id }&page=${ 1 }&limit=${ 10 }&isReportScale=1&tenantId=${ tenantId }&accountId=${accountId}&hospName=${hospName}&birthday=${birthday}`

            // url = `http://localhost:9020/#/scale/report?timestamp=${new Date().getTime()}&patientId=${ id }&page=${ 1 }&limit=${ 10 }&isReportScale=1&tenantId=${ tenantId }&accountId=${accountId}&hospName=${hospName}&birthday=${birthday}`
            console.log('报告列表',url)
        }

        /**
         * 如果是泵乳上传页面
         */
        if (url&&url.indexOf('breast/update') > -1) {
            let userId =app.globalData.userId
            url = url.replace('index.html/','index.html#/')
            wx.showLoading({
                title: '加载中...'
            });
            GET(app.getUrl('/xcx/pt/queryArchiveInfo'))
                .then(res => {
                    console.log('q',res.data.dataContent.filter(item => item.id === 'zcejd_ch')[0])
                    if(res.data.dataContent.filter(item => item.id === 'zcejd_ch')[0]){
                        const {
                            value: bedNo
                        } = res.data.dataContent.filter(item => item.id === 'zcejd_ch')[0];
                        const {
                            value: hospitalNo
                        } = res.data.dataContent.filter(item => item.id === 'Pub12x00008')[0];
                        url = `${app.globalData.config.webServer}${ url }&userId=${userId}&bedNo=${ bedNo }&hospitalNo=${ hospitalNo }&tenantId=${ tenantId }&proxy=true&&timestamp=${new Date().getTime()}`;
                        // url = `http://localhost:9020/#/breast/update?bedNo=${ bedNo }&hospitalNo=${ hospitalNo }&tenantId=${ tenantId }`;
                    }else{
                        url = `${app.globalData.config.webServer}${ url }&userId=${userId}&tenantId=${ tenantId }&proxy=true&timestamp=${new Date().getTime()}`;
                    }
                   
                   
                   
                    this.setData({
                        url:url+`?timestamp=${new Date().getTime()}`
                    });
                    wx.hideLoading();
                    console.log('泵乳url',url)
                })
                
            return;
        }
       

        // 点击菜单线上随访问卷
        if(url && url.indexOf('questionnaire/quest') >-1 ){

            url = app.globalData.config.webServer+`${ url}&deptId=${deptId}&userId=${userId}&tenantId=${ tenantId }`
            console.log('app.globalData',app.globalData)
            console.log('线上问卷aa',url)
          
        }
        
        // 点击关注公众号
        if(url && url.indexOf('payattentionto')>-1 ){
            url = app.globalData.config.webServer+url
        }

         // 点击科室宣教
         if(url && url.indexOf('knowledge/list')>-1 ){
            let userId =app.globalData.userId
            const {  userName,tenantId,gender,birthday,phoneNo } = app.globalData.userList.filter(item=>{
                return item.id = userId
            })[0];
            url = url.replace('index.html/','index.html#/')
            url = app.globalData.config.webServer+url+`&tenantId=${ tenantId }&userName=${ userName }&projectName=科普推送&createBy=xinhua&date=${options.currentDate&&options.currentDate.split(' ')[0]}&patientId=${ userId }&gender=${ gender }&birthday=${ birthday }&phoneNum=${phoneNo}&timestamp=${new Date().getTime()}`
            // url = app.globalData.config.webServer+`?userId=${ userId }&timestamp=${new Date().getTime()} +${url}`
            console.log('科普宣教url',url)
        }

        // 点击知情同意书
        if(url && url.indexOf('informedconsent/list')>-1){
            url = url.replace('index.html/','index.html#/')
            url = app.globalData.config.webServer+url+`&userId=${ userId }&tenantId=${ tenantId }&deptId=${ deptId }&proxy=false&timestamp=${new Date().getTime()}`
            //  url = "http://192.168.3.149:9020/#/informedconsent/list"+`?tenantId=${ tenantId }&deptId=${ deptId }&userId=${ userId }`
            // url = app.globalData.config.webServer+`?userId=${ userId }&timestamp=${new Date().getTime()} +${url}`
         console.log('知情同意书url',url)
        }
        // 点击体检报告
        if(url && url.indexOf('/checkUpReport/history')>-1){
            url = url.replace('index.html/','index.html#/')
            url = app.globalData.config.webServer+url+`&userId=${ userId }&tenantId=${ tenantId }&deptId=${ deptId }&proxy=false&timestamp=${new Date().getTime()}`
            // url ='http://192.168.3.149:9020/#/checkUpReport/history' +`?tenantId=${ tenantId }&deptId=${ deptId }&userId=${app.globalData.userId}`
            console.log('体检报告',url)
        }
        // 点击医嘱通知消息
        if(url && url.indexOf('/breast/advice')>-1){
            console.log('options',options)
            url = url.replace('index.html/','index.html#/')
            url = app.globalData.config.webServer+url+`&tenantId=${ tenantId }&messageNotifyId=${ options.messageNotifyId }&fuMilkPreparationId=${options.fuMilkPreparationId}`
            console.log('医嘱通知',url)
        }

         // 点击查看体检号
         if(url && url.indexOf('/physicalExam')>-1){
            console.log('options',options)
            url = url.replace('index.html/','index.html#/')
            url = app.globalData.config.webServer+url+`&healthCheckNo=${healthCheckNo}&userName=${userName}&timestamp=${new Date().getTime()}`
            // url = `http://localhost:9020/#/physicalExam?healthCheckNo=${healthCheckNo}&userName=${userName}`
            console.log('查看体检号',url)
        }
        
        // 建档
        if(url&& url.indexOf('/newArchive')>-1){
            console.log('options',options)
            url = url.replace('index.html/','index.html#/')
            let templId = options.templId
            let userMobile  = app.globalData.userMobile
            let queueId = options.queueId
            app.globalData.wxUser.ptDefaultDeptId = app.globalData.deptId
            app.globalData.wxUser.mobile = userMobile
            let jsonObj = JSON.stringify(app.globalData.wxUser);
            let tenId = app.globalData.hospitalInfo.tenantId
            jsonObj = encodeURI(jsonObj);
            console.log('url',url)
            url = app.globalData.config.webServer+url+`&templId=${templId}&userMobile=${userMobile}&queueId=${queueId}&wxUser=${jsonObj}&userId=${options.userId}&exist=${options.exist}&tenantId=${ tenId }&userName=${userName}&timestamp=${new Date().getTime()}&proxy=false`
            // url = "http://192.168.3.149:9020/#/newArchive"+`?templId=${templId}&userMobile=${userMobile}&queueId=${queueId}&openId=${app.globalData.openid}&wxUser=${jsonObj}&userId=${options.userId}`
            console.log('建档url',url)
        }
        // 主诉问卷
        if(url&& url.indexOf('/ccquestionnaire')>-1){
   
            url = url.replace('index.html/','index.html#/')
 

            let queueId = app.globalData.queueId
            let userId = app.globalData.userId

            console.log('url',url)
            // url = app.globalData.config.webServer+url+`&templId=${templId}&userMobile=${userMobile}&queueId=${queueId}&wxUser=${jsonObj}&userId=${options.userId}&exist=${options.exist}&tenantId=${ tenId }&userName=${userName}&timestamp=${new Date().getTime()}&proxy=false`
            url = url+`&deptId=${deptId}&userId=${userId}&tenantId=${ tenantId }&queueId=${queueId}`

            // url = "http://192.168.3.149:9020/#/ccquestionnaire"+`?templId=${templId}&userMobile=${userMobile}&queueId=${queueId}&openId=${app.globalData.openid}&userId=${options.userId}&tenantId=${ tenId }&proxy=false`
            // console.log('主诉问卷url',url)
        }

        
        // 康复治疗计划单
        if(url&& url.indexOf('treatmentplanss')>-1){
   

          let userId =app.globalData.userId
          const {  userName,tenantId,gender,birthday,phoneNo } = app.globalData.userList.filter(item=>{
              return item.id = userId
          })[0];

          let queueId = app.globalData.queueId

          console.log('url',url)
          // url = app.globalData.config.webServer+url+`&templId=${templId}&userMobile=${userMobile}&queueId=${queueId}&wxUser=${jsonObj}&userId=${options.userId}&exist=${options.exist}&tenantId=${ tenId }&userName=${userName}&timestamp=${new Date().getTime()}&proxy=false`
          url =app.globalData.config.webServer+ url+`&deptId=${deptId}&userId=${userId}&tenantId=${ tenantId }&queueId=${queueId}&gender=${ gender }&birthday=${ birthday }&userName=${ userName }`

          // url = "http://192.168.3.149:9020/#/ccquestionnaire"+`?templId=${templId}&userMobile=${userMobile}&queueId=${queueId}&openId=${app.globalData.openid}&userId=${options.userId}&tenantId=${ tenId }&proxy=false`
          // console.log('主诉问卷url',url)
        }
      

        // 治疗卡
        if(url&& url.indexOf('treatmentcard')>-1){
   

          let userId =app.globalData.userId
          const {  userName,tenantId,gender,birthday,phoneNo } = app.globalData.userList.filter(item=>{
              return item.id = userId
          })[0];

          let queueId = app.globalData.queueId

          console.log('url',url)
          // url = app.globalData.config.webServer+url+`&templId=${templId}&userMobile=${userMobile}&queueId=${queueId}&wxUser=${jsonObj}&userId=${options.userId}&exist=${options.exist}&tenantId=${ tenId }&userName=${userName}&timestamp=${new Date().getTime()}&proxy=false`
          url =app.globalData.config.webServer+ url+`&deptId=${deptId}&userId=${userId}&tenantId=${ tenantId }&queueId=${queueId}&gender=${ gender }&birthday=${ birthday }&userName=${ userName }`

          // url = "http://192.168.3.149:9020/#/ccquestionnaire"+`?templId=${templId}&userMobile=${userMobile}&queueId=${queueId}&openId=${app.globalData.openid}&userId=${options.userId}&tenantId=${ tenId }&proxy=false`
          // console.log('主诉问卷url',url)
        }


        // 评估报告
        if(url&& url.indexOf('assessment')>-1){
   

        let userId =app.globalData.userId
        const {  userName,tenantId,gender,birthday,phoneNo } = app.globalData.userList.filter(item=>{
            return item.id = userId
        })[0];

        let queueId = app.globalData.queueId

        console.log('url',url)
        // url = app.globalData.config.webServer+url+`&templId=${templId}&userMobile=${userMobile}&queueId=${queueId}&wxUser=${jsonObj}&userId=${options.userId}&exist=${options.exist}&tenantId=${ tenId }&userName=${userName}&timestamp=${new Date().getTime()}&proxy=false`
        url =app.globalData.config.webServer+ url+`&deptId=${deptId}&userId=${userId}&tenantId=${ tenantId }&queueId=${queueId}&gender=${ gender }&birthday=${ birthday }&userName=${ userName }`

        // url = "http://192.168.3.149:9020/#/ccquestionnaire"+`?templId=${templId}&userMobile=${userMobile}&queueId=${queueId}&openId=${app.globalData.openid}&userId=${options.userId}&tenantId=${ tenId }&proxy=false`
        // console.log('主诉问卷url',url)
        }
        







        that.setData({
            url:url
        })
        console.log('url11',url)
    },

  

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {
        if (wx.getStorageSync('paySuccessUrl')) {
            this.setData({
                url: wx.getStorageSync('paySuccessUrl')
            })
            wx.removeStorageSync('paySuccessUrl')
        }
    },


    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function (e) {
        // return e
        // return {
            console.log('e22',e)
            let href = decodeURIComponent(e.webViewUrl)
            href = href.split('#/scale/paper?')[1]
            href = href.split('&')
            let param = {}
            href.forEach(function (item) {
              param[item.split('=')[0]] = item.split('=')[1]
            })
           
           console.log('param',param)
            let h5Url1 = decodeURIComponent(e.webViewUrl).split('?')[0]
            let h5Url2 = decodeURIComponent(e.webViewUrl).split('?')[1].replace(/&/g,',')
            console.log('h5Url1',h5Url1)
            console.log('h5Url2',h5Url2)
           let { planId,patientId,paperId,status,tenantId,birthday,followPatientId,patientName,gender,patId,title} = param
            return {
                path:`/pages/share/index?path1=${h5Url1}&path2=${h5Url2}&planId=${planId}&patientId=${ patientId }&paperId=${ paperId }&status=${ status }&tenantId=${ tenantId }&birthday=${ birthday }&followPatientId=${ followPatientId }&patientName=${ patientName }&gender=${ gender }&patId=${ patId }&title=${ title }`
            }
     
    },
    
    //h5向小程序发送的数据
    h5PostMessage: function (e) {
        this.h5Data=  e.detail.data;  //当用户点击这个h5页面的分享时获取到
    },
})